package com.woniu.shop.service.impl;

import com.woniu.constant.Constant;
import com.woniu.entity.Result;
import com.woniu.entity.ResultCode;
import com.woniu.shop.entity.dto.ServiceEvaluateDTO;
import com.woniu.shop.mapper.StoreServiceMapper;
import com.woniu.shop.pojo.ServiceEvaluate;
import com.woniu.shop.mapper.ServiceEvaluateMapper;
import com.woniu.shop.service.IServiceEvaluateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务评价表 服务实现类
 * </p>
 *
 * @author malei
 * @since 2023-02-17
 */
@Service
public class ServiceEvaluateServiceImpl extends ServiceImpl<ServiceEvaluateMapper, ServiceEvaluate> implements IServiceEvaluateService {

    @Autowired
    private ServiceEvaluateMapper serviceEvaluateMapper;

    @Autowired
    private StoreServiceMapper storeServiceMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public Result addServiceEvaluate(ServiceEvaluateDTO serviceEvaluateDTO) {
        Field[] fields = serviceEvaluateDTO.getClass().getDeclaredFields();
        for (Field field: fields) {
            field.setAccessible(true);
            String methodName = "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
            Method method = null;
            try {
                method = serviceEvaluateDTO.getClass().getMethod(methodName);
                Object value = method.invoke(serviceEvaluateDTO);
                System.out.println(method.getName()+"=======");
                if (value == null ||value.toString().length() == 0) {
                    return Result.error(ResultCode.ERROR.getCode(), "请检查申请信息是否填写完整");
                }
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        ServiceEvaluate serviceEvaluate = new ServiceEvaluate();
        serviceEvaluate.setIsDeleted("0");
        serviceEvaluate.setEvaluateStatus("0");
        BeanUtils.copyProperties(serviceEvaluateDTO,serviceEvaluate);
        int flag = serviceEvaluateMapper.insert(serviceEvaluate);
        int star = 0;
        if (flag == 1){
            List<ServiceEvaluate> evaluateList = serviceEvaluateMapper.selectByServiceId(serviceEvaluateDTO.getServiceId());
            for (ServiceEvaluate evaluate : evaluateList) {
                star+=evaluate.getServiceStar();
            }
            System.out.println("==========="+star);
            BigDecimal bigDecimal = new BigDecimal(star);
            BigDecimal num = new BigDecimal(evaluateList.size());
            BigDecimal score = bigDecimal.divide(num,1,BigDecimal.ROUND_HALF_UP);
            System.out.println("========="+score);
            storeServiceMapper.setServiceStar(score,serviceEvaluateDTO.getServiceId());
            stringRedisTemplate.opsForSet().add(Constant.REDIS_DB_UPLOAD_IMAGE,serviceEvaluate.getEvaluatePicture());
            return Result.ok(ResultCode.SUCCESS.getCode(),"添加成功",flag);
        }
        return Result.error(ResultCode.ERROR.getCode(), "请检查申请信息是否填写完整");
    }

    @Override
    public Result queryServiceEvaluateById(String id,String status) {

        List<ServiceEvaluate> serviceEvaluates = serviceEvaluateMapper.queryServiceEvaluateById(id,status);
        if (serviceEvaluates.size() > 0 && serviceEvaluates != null) {
            return Result.ok(serviceEvaluates);
        }
        return Result.error(ResultCode.RESULE_DATA_NONE.getCode(),ResultCode.RESULE_DATA_NONE.getMessage());
    }

    @Override
    public Result replyEvaluate(String id, String message) {
        int num = serviceEvaluateMapper.replyEvaluate(id,message);
        if (num > 0 ) {
            return Result.ok();
        }
        return Result.error(ResultCode.ERROR.getCode());
    }

    @Override
    public Result queryByUserId(String id, String status) {
        List<ServiceEvaluate> serviceEvaluates = serviceEvaluateMapper.queryByUserId(id,status);
        if (serviceEvaluates.size() > 0 && serviceEvaluates != null ) {
            return Result.ok(serviceEvaluates);
        }
        return Result.error(ResultCode.ERROR.getCode());
    }
}
